Ext.define("MyApp.controller.Setting",{
    extend: "Ext.app.Controller",
	config: {
		views: [
			'SettingsPanel'
		],
		models: [
			'Settings'
		],
		stores: [
			'Settings',
			'LocalSettings'
		],
		control: {
			'settingspanel': {
				initialize: "SettingsLoad"
			},
			'settingspanel button#save': {
				tap: "SaveSettings"
			},
			'settingspanel button#cancel': {
				tap: function() {
					
					Ext.Viewport.setActiveItem('appointmentlist',{type:'slide', direction:'right'});
				}
			}
		}
	},
	
	SettingsLoad: function() {
	
		var storage = window.localStorage;
		var userID = storage.getItem("UserID");
		
		var userIdField = Ext.ComponentQuery.query('settingspanel hiddenfield[itemId="userId"]').pop();
		userIdField.setValue(userID);
		console.log("userId" + userID);
		var localstore = Ext.getStore("LocalSettings");
		localstore.load();
		console.log(localstore.getCount());
		if ((localstore.getCount())== 0) {
			console.log("inside web req");
			var store = Ext.getStore("Settings");
			var proxy = store.getProxy();
			proxy.setExtraParam("id", userID);
			store.load({
					callback: function(records, operation, success) {
					Ext.each(records,function(record){
						record.dirty = true;
					});
					localstore.add(records);
					localstore.sync();	
					
					var record = localstore.getAt(0);
					if  (typeof record != 'undefined'){
						var form = Ext.ComponentQuery.query('settingspanel formpanel[itemId="settingsForm"]').pop();
						form.setRecord(record);
						var applength = Ext.ComponentQuery.query('settingspanel selectfield[itemId="applength"]').pop();
						applength.setValue(record.data.applength);
					}
				}
			});	
				
		}
		else {
			var record = localstore.getAt(0);
			if  (typeof record != 'undefined'){
				var form = Ext.ComponentQuery.query('settingspanel formpanel[itemId="settingsForm"]').pop();
				form.setRecord(record);
				var applength = Ext.ComponentQuery.query('settingspanel selectfield[itemId="applength"]').pop();
				applength.setValue(record.data.applength);
			}
		}
		
		
	
	},
	
	SaveSettings: function() {
		var form = Ext.ComponentQuery.query('settingspanel formpanel[itemId="settingsForm"]').pop();
		var values = form.getValues();
		console.log("inside save settings");
		//values.applength = 30;
		console.log(values.applength);
		Ext.Ajax.request({
			url: 'http://appt.elasticbeanstalk.com/settings.php',
			params: values,
			success: function(response){
				var text = response.responseText;
				var result = Ext.decode(text);
				var data = Ext.JSON.decode(response.responseText.trim());
				
				if (data.Success == 'true'){
					console.log("data.success = true");
					//refresh local store
					 var localstore = Ext.getStore("LocalSettings");
						
					values.sendmap = data.sendMap;
					values.sendcalendar = data.sendCalendar;
					values.sendreminder = data.sendReminder;
					values.calendar = data.calendar;
					localstore.removeAll();
					localstore.sync();
					var newRecord = new MyApp.model.Settings(values);
					localstore.add(newRecord);
					localstore.sync();
					
					Ext.Viewport.setActiveItem('appointmentlist',{type:'slide', direction:'right'});

				}
			},
			failure:function(response){
				console.log(response.responseText); 
			}
		});
	}
	
});